package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-12
 */
@Api(description = "登陆注册")
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @ApiOperation("登录")
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        //用service方法实现登录，返回token值
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    @ApiOperation("注册")
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation("获取用户信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        if(!JwtUtils.checkToken(request)) {
            return R.error().message("token非法");
        }
        //调用jwt工具，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        //查询数据库，获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    //根据token字符串获取用户信息
    @ApiOperation("根据id获取用户信息")
    @PostMapping("getInfo/{id}")
    public UcenterMember getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = memberService.getById(id);
        System.out.println(ucenterMember);
        UcenterMember memeber = new UcenterMember();
        BeanUtils.copyProperties(ucenterMember,memeber);
        System.out.println(memeber);
        return memeber;
    }


    @ApiOperation("order获取用户信息")
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember member = memberService.getById(id);
        System.out.println("++++++++++++++++++++\n" + member + "\n++++++++++++++++++++\n");
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        System.out.println("++++++++++++++++++++\n" + ucenterMemberOrder + "\n++++++++++++++++++++\n");
        return ucenterMemberOrder;
    }

    @ApiOperation("查询某一天注册人数")
    @PostMapping("counterRegister/{day}")
    public R counterRegister(@PathVariable String day) {

        Integer count = memberService.counterRegisterByDay(day);
        return R.ok().data("countRegister",count);

    }




}

